package cn.bob.basic.listener;

import cn.bob.basic.jwt.JwtRasHolder;
import cn.bob.basic.jwt.LoginContext;
import cn.bob.system.service.IPermissionService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.FileCopyUtils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * @author bob
 * @data 2022/10/24-23:28
 */
//声明监听器
@WebListener
@Slf4j
public class Loginlistener implements ServletContextListener {

    @Value("${jwt.rsa.pub}")
    private String jwtRsaPub;
    @Value("${jwt.rsa.pri}")
    private String jwtRsaPri;

    @SneakyThrows
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        log.info("System is initing");
             JwtRasHolder.INSTANCE.setPubKeyData(FileCopyUtils.copyToByteArray(
                      this.getClass().getClassLoader().getResourceAsStream(jwtRsaPub)));
              JwtRasHolder.INSTANCE.setPriKeyData(FileCopyUtils.copyToByteArray(
                      this.getClass().getClassLoader().getResourceAsStream(jwtRsaPri)));
        log.info("System is init finished!");
    } // 程序停止时被调用

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        // 主动打印日志
        log.info("System is  Destroyed!");
    }
}
